/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.taglib;

import org.apache.commons.beanutils.PropertyUtils;

import org.apache.log4j.Logger;

import org.apache.struts.taglib.TagUtils;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;


/**
 * Generate mailto strings based on the list of object having
 * mail attribute.
 * This tag ensures that only one occurence of a mail address will be
 * used (double detection performed).
 *
 */
public class MailToTag extends TagSupport
{
    /** Logger. */
    private static Logger _logger = Logger.getLogger(MailToTag.class);

    /** Name of the object containing the list containing the objects. */
    private String name;

    /** Scope of the object containing the list containing the objects. */
    private String scope;

    /** Generated label. */
    private String label;

//DW/2619/BeginPatch
    /** Mail attribute. */
    private String mailname = "mail";
//DW/2619/EndPatch

    /**
     * @see javax.servlet.jsp.tagext.Tag#doStartTag().
     */
    @Override
	public int doStartTag() throws JspException
    {
        List liste;

        try
        {
            liste = (List) TagUtils.getInstance().lookup(pageContext, name, null,
                    scope);
        }
        catch (Exception e)
        {
            return (SKIP_BODY); // Nothing to output
        }

        if ((liste == null) || (liste.isEmpty()))
        {
            return (SKIP_BODY); // Nothing to output
        }

        // detection de doublon
        Set mailSet = new HashSet();

        Iterator it = liste.iterator();
        String mail;

        while (it.hasNext())
        {
            Object next = it.next();
            try
            {
//DW/2619/BeginPatch
                //mail = (String) PropertyUtils.getProperty(next, "mail");
                mail = (String) PropertyUtils.getProperty(next, mailname);
//DW/2619/EndPatch

                if (mail != null)
                {
                    mailSet.add(mail);
                }
            }
            catch (Exception e)
            {
                // do nothing , just continue looping
                _logger.debug("No mail for " + next);
            }
        }

        // create the string, if the mailSet empty just return empty
        if (mailSet.isEmpty())
        {
            return (SKIP_BODY); // Nothing to output
        }

        // mailSet not empty
        StringBuffer sb = new StringBuffer();
        sb.append("<a href='mailto:");

        it = mailSet.iterator();

        while (it.hasNext())
        {
            mail = (String) it.next();
            sb.append(mail);
            sb.append(';');
        }

        sb.append("'>");
        sb.append(label);
        sb.append("</a>");

        TagUtils.getInstance().write(pageContext, sb.toString());

        return SKIP_BODY;
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#release().
     */
    @Override
	public void release()
    {
        super.release();
    }

    /**
     * Sets the name of the object containing the list containing the objects.
     * @param aname The name to set
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**
     * Sets the scope of the object containing the list containing the objects.
     * @param ascope The scope to set
     */
    public void setScope(String ascope)
    {
        this.scope = ascope;
    }

    /**
     * Sets the label to be generated by the tag.
     * @param alabel The label to set
     */
    public void setLabel(String alabel)
    {
        this.label = alabel;
    }

//DW/2619/BeginPatch
    /**
     * Sets the name of the mail attribute. Default is "mail".
     * @param amailname The attribute name to set.
     */
    public void setMailname(String amailname)
    {
        this.mailname = amailname;
    }
//DW/2619/EndPatch

}
